syntax = "proto3";

package kuma.mesh.v1alpha1;

option go_package = "github.com/kumahq/kuma/api/mesh/v1alpha1";

import "api/mesh/options.proto";
import "api/mesh/v1alpha1/selector.proto";
import "kuma-doc/config.proto";

option (doc.config) = {
  type : Policy,
  name : "ProxyTemplate",
  file_name : "proxy-template"
};

// ProxyTemplate defines the desired state of ProxyTemplate
message ProxyTemplate {

  option (kuma.mesh.resource).name = "ProxyTemplateResource";
  option (kuma.mesh.resource).type = "ProxyTemplate";
  option (kuma.mesh.resource).package = "mesh";
  option (kuma.mesh.resource).kds.send_to_zone = true;
  option (kuma.mesh.resource).ws.name = "proxytemplate";
  option (kuma.mesh.resource).allow_to_inspect = true;

  // List of Dataplane selectors.
  repeated Selector selectors = 1 [ (doc.required) = true ];

  // Configuration for ProxyTemplate
  Conf conf = 2 [ (doc.required) = true ];

  message Conf {
    // List of imported profiles.
    // +optional
    repeated string imports = 1;

    // List of raw xDS resources.
    // +optional
    repeated ProxyTemplateRawResource resources = 2;

    // List of config modifications
    repeated Modifications modifications = 3;
  }

  // Modifications to xDS config generated by Proxy Template
  message Modifications {
    oneof type {
      // Cluster modification
      Cluster cluster = 1;
      // Listener modification
      Listener listener = 2;
      // Network Filter modification
      NetworkFilter networkFilter = 3;
      // HTTP Filter modification
      HttpFilter httpFilter = 4;
      // Virtual Host modifications
      VirtualHost virtualHost = 5;
    }

    // Cluster defines modifications to generated clusters
    message Cluster {
      // Only clusters that match will be modified
      Match match = 1;
      // Operation to apply on a cluster (add, remove, patch)
      string operation = 2 [ (doc.required) = true ];
      // xDS cluster
      string value = 3;

      // Match defines match for cluster
      message Match {
        // Origin of the resource generation. (inbound, outbound, prometheus,
        // transparent, ingress)
        string origin = 1;
        // Name of the cluster to match
        string name = 2 [ (doc.required) = true ];
      }
    }

    // Listener defines modification to generated listeners
    message Listener {
      // Only listeners that match will be modified
      Match match = 1;
      // Operation to apply on a listener (add, remove, patch)
      string operation = 2 [ (doc.required) = true ];
      // xDS listener
      string value = 3;

      // Match defines match for listener
      message Match {
        // Origin of the resource generation. (inbound, outbound, prometheus,
        // transparent, ingress)
        string origin = 1;
        // Name of the listener to match
        string name = 2 [ (doc.required) = true ];
        // Tags available in Listener#Metadata#FilterMetadata[io.kuma.tags]
        map<string, string> tags = 4;
      }
    }

    // Listener defines modification to generated network filters
    message NetworkFilter {
      // Only network filters that match will be modified
      Match match = 1;
      // Operation to apply on network filter (addFirst, addLast, addBefore,
      // addAfter, remove, patch)
      string operation = 2 [ (doc.required) = true ];
      // xDS network filter
      string value = 3;

      // Match defines match for network filter
      message Match {
        // Origin of the resource generation. (inbound, outbound, prometheus,
        // transparent, ingress)
        string origin = 1;
        // Name of the network filter
        string name = 2 [ (doc.required) = true ];
        // Name of the listener that network filter modifications will be
        // applied to
        string listenerName = 3;
        // ListenerTags available in
        // Listener#Metadata#FilterMetadata[io.kuma.tags]
        map<string, string> listenerTags = 4;
      }
    }

    // HttpFilter defines modifications to generated HTTP filters
    message HttpFilter {
      // Only HTTP filters that match will be modified
      Match match = 1;
      // Operation to apply on network filter (addFirst, addLast, addBefore,
      // addAfter, remove, patch)
      string operation = 2 [ (doc.required) = true ];
      // xDS HTTP filter
      string value = 3;

      // Match defines match for http filter
      message Match {
        // Origin of the resource generation. (inbound, outbound, prometheus,
        // transparent, ingress)
        string origin = 1;
        // Name of the network filter
        string name = 2;
        // Name of the listener that http filter modifications will be applied
        // to
        string listenerName = 3;
        // ListenerTags available in
        // Listener#Metadata#FilterMetadata[io.kuma.tags]
        map<string, string> listenerTags = 4;
      }
    }

    // VirtualHost defines modification to generated virtual hosts
    message VirtualHost {
      // Only virtual hosts that match will be modified
      Match match = 1;
      // Operation to apply on a virtual hosts (add, remove, patch)
      string operation = 2 [ (doc.required) = true ];
      // xDS virtual host
      string value = 3;

      // Match defines match for virtual host
      message Match {
        // Origin of the resource generation. (inbound, outbound, prometheus,
        // transparent, ingress)
        string origin = 1;
        // Name of the virtual host to match
        string name = 2 [ (doc.required) = true ];
        // Name of the route configuration
        string routeConfigurationName = 3;
      }
    }
  }
}

message ProxyTemplateSource {

  // Name of a configuration source.
  // +optional
  string name = 1;

  oneof type {

    // Profile, e.g. `default-proxy`.
    // +optional
    ProxyTemplateProfileSource profile = 2;

    // Raw xDS resources.
    // +optional
    ProxyTemplateRawSource raw = 3;
  }
}

message ProxyTemplateProfileSource {

  // Profile name.
  string name = 1;

  // Profile params if any.
  // +optional
  map<string, string> params = 2;
}

message ProxyTemplateRawSource {

  // List of raw xDS resources.
  // +optional
  repeated ProxyTemplateRawResource resources = 1;
}

message ProxyTemplateRawResource {

  // The resource's name, to distinguish it from others of the same type of
  // resource.
  string name = 1 [ (doc.required) = true ];

  // The resource level version. It allows xDS to track the state of individual
  // resources.
  string version = 2 [ (doc.required) = true ];

  // xDS resource.
  string resource = 3 [ (doc.required) = true ];
}
